package com.example.wengwyue.patientapplication.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wengwyue.patientapplication.R;
import com.example.wengwyue.patientapplication.fragment.CurrentPatientFragment;
import com.example.wengwyue.patientapplication.fragment.PayFragment;
import com.example.wengwyue.patientapplication.fragment.VisitedDoctorFragment;
import com.example.wengwyue.patientapplication.fragment.VisitedHospitalFragment;


/**
 * 作者： wenyue   时间： 16/6/4.
 * 功能： 主页面
 * 修改：
 */

public class MainActivity extends BaseVaryViewActivity implements View.OnClickListener {
    public static final int BAR_HOSPITAL = 0;
    public static final int BAR_DOCTOR = 1;
    public static final int BAR_PATIENT = 2;
    public static final int BAR_PAY = 3;

    private View mCurrentView;
    private int MAX_SIZE = 4;
    private static final String TAG = "MainActivity";
    private Fragment[] mFragments;
    private Fragment mCurrentFragment;
    private View mBarHospital, mBarDoctor, mBarPatient, mBarPay;
    private TextView titleText;
    private ImageView titleRightImage;
    private View mContentView;


    @Override
    protected int provideLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mContentView = findViewById(R.id.main_content);
        mBarHospital = findViewById(R.id.main_bar_visited_hospital);
        mBarDoctor = findViewById(R.id.main_bar_visited_doctor);
        mBarPatient = findViewById(R.id.main_bar_current_patient);
        mBarPay = findViewById(R.id.main_bar_pay);
        titleText = (TextView) findViewById(R.id.title_text);
        titleRightImage = (ImageView) findViewById(R.id.title_right_image);
        super.initView(savedInstanceState);
    }

    @Override
    public View provideDataView() {
        return mContentView;
    }

    @Override
    protected void initListener() {
        mBarHospital.setOnClickListener(this);
        mBarDoctor.setOnClickListener(this);
        mBarPatient.setOnClickListener(this);
        mBarPay.setOnClickListener(this);
        findViewById(R.id.title_right_image).setOnClickListener(this);
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        //默认选择第一个
        findViewById(R.id.main_bar_visited_hospital).performClick();

        selectBar(BAR_HOSPITAL);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_bar_visited_hospital:
                onClickBar(v, BAR_HOSPITAL);
                break;
            case R.id.main_bar_visited_doctor:
                onClickBar(v, BAR_DOCTOR);
                break;
            case R.id.main_bar_current_patient:
                onClickBar(v, BAR_PATIENT);
                break;
            case R.id.main_bar_pay:
                onClickBar(v, BAR_PAY);
                break;
        }
    }

    /**
     * 点击bar的处理
     *
     * @param v 点击的bar view
     * @param i 索引
     */
    private void onClickBar(View v, int i) {
        //如果还是当期tab不需要切换
        if (v == null || v.equals(mCurrentView)) {
            return;
        }
        changeTitle(i);
        //单选控制
        if (mCurrentView != null) {
            mCurrentView.setSelected(false);
        }
        v.setSelected(true);
        //根据经验，提交碎片是有可能崩溃的
        try {
            changeTopNavigationBar(i);
        } catch (Exception e) {
//            LogUtil.e(sTAG, e.getMessage()); // 打log
        }
        mCurrentView = v;
    }

    /**
     * 点击了才去初始化碎片
     *
     * @param tab tab
     */
    public void changeTopNavigationBar(int tab) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

        if (mFragments == null) {
            mFragments = new Fragment[MAX_SIZE];
        }
        switch (tab) {
            case BAR_HOSPITAL:
                if (mFragments[tab] == null) {
                    mFragments[tab] = new VisitedHospitalFragment();
                    fragmentTransaction.add(R.id.main_content, mFragments[tab],
                            String.valueOf(tab));
                }
                break;
            case BAR_DOCTOR:
                if (mFragments[tab] == null) {
                    mFragments[tab] = new VisitedDoctorFragment();
                    fragmentTransaction.add(R.id.main_content, mFragments[tab],
                            String.valueOf(tab));
                }


                break;
            case BAR_PATIENT:
                if (mFragments[tab] == null) {
                    mFragments[tab] = new CurrentPatientFragment();
                    fragmentTransaction.add(R.id.main_content, mFragments[tab],
                            String.valueOf(tab));
                }
                break;
            case BAR_PAY:
                if (mFragments[tab] == null) {
                    mFragments[tab] = new PayFragment();
                    fragmentTransaction.add(R.id.main_content, mFragments[tab],
                            String.valueOf(tab));
                }
                break;

        }

        for (int i = 0; i < MAX_SIZE; i++) {
            Fragment fragment = mFragments[i];
            if (i != tab) {
                if (fragment != null) {
                    fragmentTransaction.hide(fragment);
                }
            } else {
                fragmentTransaction.show(fragment);
                mCurrentFragment = fragment;
            }
        }
        fragmentTransaction.commitAllowingStateLoss();
    }

    /**
     * 选择bar，给非点击选择用
     *
     * @param barHome bar
     */
    private void selectBar(int barHome) {
        changeTitle(barHome); // 修改标题栏
        switch (barHome) {
            case BAR_HOSPITAL:
                if (mBarHospital != null) {
                    mBarHospital.performClick();
                }
                break;
            case BAR_DOCTOR:
                if (mBarDoctor != null) {
                    mBarDoctor.performClick();
                }
                break;
            case BAR_PATIENT:
                if (mBarPatient != null) {
                    mBarPatient.performClick();
                }
                break;
            case BAR_PAY:
                if (mBarPay != null) {
                    mBarPay.performClick();
                }

        }
    }

    private void changeTitle(int tab) {
        switch (tab) {
            case BAR_HOSPITAL:
                titleText.setText(R.string.main_tab_visited_hospital);
                break;
            case BAR_DOCTOR:
                titleText.setText(R.string.main_tab_visited_doctor);
                break;
            case BAR_PATIENT:
                titleText.setText(R.string.main_tab_current_patient);
                break;
            case BAR_PAY:
                titleText.setText(R.string.main_tab_pay);
                break;

        }

    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
